# coding=utf-8

#加载配置文件
import  config
#加载函数库
from base_fun import base_fun;
#加载系统模块
import socket,time,random,sys,types;
#加载mysql数据库
import MySQLdb;
#多线程
import threading;
#任务队列
import Queue;

#config.debug = True;

#数据库hash
base_fun = base_fun(config.error_msg,db_len=config.db_hash_len);
db_hash = config.db_hash;
#全部任务队列
sql_job_list={};

def sub_insert(db_hash):
	#数据库连接
	db_list = config.insert_cli[db_hash];
	db_name = config.insert_db_name+db_hash;
	db_host = db_list['host'];
	db_user = db_list['user'];
	db_pwd = db_list['pwd'];
	try:
		s_con=MySQLdb.Connection(host=db_host,user=db_user,passwd=db_pwd,db=db_name,charset=config.db_charset,connect_timeout=config.mysql_connect_timeout);
		s_db =  s_con.cursor();
	except MySQLdb.Error, e:
		print "%s connect Error %d: %s" % (db_hash,e.args[0], e.args[1]);
		sys.exit(1);
	#socket连接
	try:
		scon = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
		scon.connect((config.make_index_host, config.make_index_port));
	except:
		if config.debug:
			base_fun.show_except();
		sys.exit(1);
	while True:
		#任务处理
		try:
			temp = sql_job_list[db_hash].get(False);
			sql = temp['sql'];
			row = temp['data'];
		except Queue.Empty:
			time.sleep(0.1);
			continue;
		try:
			s_db.execute(sql);
			#执行sql成功，处理socket数据
			try:
				uid = row[config.insert_field_uid];
				mc_id = row[config.insert_field_mc_id];
				to_uid = row[config.insert_field_to_uid];
				mutuality_id = row[config.insert_field_mutuality];

				if row[config.insert_field_type] ==1:
					#梳理索引数据
					row_data = [];
					for k, v in config.insert_index_field_hash.items():
						row_data.append(row[v]);
					
					#主贴操作
					#用户 主贴
					scon.send(base_fun.build_cmd(2001,{'id':uid,'data':row_data}));
					#处理话题的关系
					topic_list = row[config.insert_field_topic].split(',');
					for topic in topic_list:
						if topic!="":
							topic_id = base_fun.get_md5(topic);
							scon.send(base_fun.build_cmd(2005,{'id':topic_id,'data':[mc_id,uid]}));
				elif row[config.insert_field_type]==2:
					#跟帖操作
					#主贴与跟帖
					scon.send(base_fun.build_cmd(2009,{'id':mutuality_id,'data':[mc_id,uid,to_uid]}));
					#用户与跟帖
					scon.send(base_fun.build_cmd(2013,{'id':uid,'data':[mc_id,mutuality_id,to_uid]}));
					#被评论用户与跟帖的关系
					scon.send(base_fun.build_cmd(2017,{'id':to_uid,'data':[mc_id,mutuality_id,uid]}));

			except IndexError,e:
				print "make index error";
				continue;
		except MySQLdb.Error, e:
			log_msg = sql;
			log_type = 'sql';
			log_level = 'error';
			base_fun.log(log_msg,log_type,log_level);
			#关闭连接
			s_db.close();
			s_con.close();
			#输出异常
			print "%s connect Error %d: %s" % (db_hash,e.args[0], e.args[1]);
			#重新连接数据库
			s_con=MySQLdb.Connection(host=db_host,user=db_user,passwd=db_pwd,db=db_name,charset=config.db_charset,connect_timeout=config.mysql_connect_timeout);
			s_db =  s_con.cursor();



for temp in db_hash:
	#根据库的数量建立任务数组
	sql_job_list[temp] = Queue.Queue(0);
	#根据书库的数量启动多进程
	t = threading.Thread(target=sub_insert,kwargs={'db_hash':temp});
	t.start();

#主数据库连接
try:
	con=MySQLdb.Connection(host=config.insert_server['host'],user=config.insert_server['user'],passwd=config.insert_server['pwd'],db=config.insert_server['db_name'],charset=config.db_charset,connect_timeout=config.mysql_connect_timeout);
	db =  con.cursor();
except MySQLdb.Error, e:
	print "main Error %d: %s" % (e.args[0], e.args[1])
	sys.exit (1)

while True:
	#循环判断任务队列里面的任务
	jump_lock = False;
	for k, jobs in sql_job_list.items():
		if jobs.qsize()>0:
			if config.debug:
				print "=======================================================";
				print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()));
				print k+":"+str(jobs.qsize());
				print "=======================================================";
			jump_lock = True;
	if jump_lock:
		time.sleep(1);
		continue;
	#更新数据
	db.execute(config.update_field_sql);
	#获取数据
	db.execute(config.get_field_sql);
	rows = db.fetchall();
	#如果当前数据未 0
	if db.rowcount ==0:
		if config.debug:
			print "=======================================================";
			print "dabases not data";
			print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()));
			print db.rowcount;
			for k, jobs in sql_job_list.items():
				print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()));
				print k+":"+str(jobs.qsize());
			print "=======================================================";
		time.sleep(1);
		continue;

	#删除数据
	db.execute(config.del_field_sql);
	#数据处理
	for row in rows:
		#传递给索引

		#进入数据库
		#取得数据库hash值
		c_db_hash =  row[config.insert_field_db];
		#获取表名
		c_table =  row[config.insert_field_tb]+config.insert_table_name;
		#声明插入语句
		insert_sql  = "INSERT INTO `"+c_table+"` SET ";
		#根据后台设置拼接sql语句
		temp_list =[];
		for k, v in config.insert_field_hash.items():
			if k==config.insert_field_db or k==config.insert_field_tb:
				continue;
			temp_sql = "%s='%s'" %(k,row[v]);
			temp_list.append(temp_sql);
		insert_sql += ','.join(temp_list);
		#print insert_sql;
		sql_job_list[c_db_hash].put({'sql':insert_sql,'data':row});
	if config.debug:
		print "=======================================================";
		print "new";
		print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()));
		print db.rowcount;
		for k, jobs in sql_job_list.items():
			print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()));
			print k+":"+str(jobs.qsize());
		print "=======================================================";

	